package com.spsy.pay.contorller.api;

import com.alibaba.fastjson2.JSON;
import com.spsy.api.common.vo.AddOrderVo;
import com.spsy.common.core.domain.R;
import com.spsy.pay.api.PayApi;
import com.spsy.pay.domain.PaymentOrder;
import com.spsy.pay.order.WeiXinUtil;
import com.spsy.pay.service.IPaymentOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author: liuzhiping
 * @date: 2025/5/28 16:20
 **/
@RestController
@Slf4j
public class PayApiController implements PayApi {


    @Autowired
    private IPaymentOrderService iPaymentOrderService;



    @Override
    public R payOrder(AddOrderVo vo)  throws Exception{
        log.info("支付系统 参数 vo:{}", JSON.toJSONString(vo));

        /*PaymentOrder pay = new PaymentOrder();
        pay.setAmount();*/

        PaymentOrder paymentOrder = PaymentOrder.builder()
                .amount(new BigDecimal("0"))
                .currency("cny")
                .orderNo(vo.getOrderId())
                .merchantId( (long)100001)
                .expireTime(new Date())
                .subject("测试")
                .userId(Long.valueOf(vo.getUserId()))
                .returnUrl("www.baidu.com")
                .notifyUrl("www.baidu.com")
                .amount(new BigDecimal(vo.getPrice()))
                .status((long)1)
                .body("测试数据").build();

        //boolean boo =  paymentOrderService.save(paymentOrder);

        int count = iPaymentOrderService.insertPaymentOrder(paymentOrder);

        log.info("count:{}",count);

        String url = WeiXinUtil.payOrder(vo.getOrderId());


        log.info("url:{}",url);


        return R.ok(url);
    }
}
